<?php
$user_id = $GLOBALS['user_id'];

$user = new user($user_id); 
$user_info[$user_id] = $user->load_tables('user_settings');

$farm_item = $db->return_first_result("SELECT * FROM `farm_items`");

function farm_updateAnimals ()
{
    global $farm_item,$db;
    
    $sql = "UPDATE `user_land`,`farm_items` SET user_land.amount_of_animals=user_land.amount_of_animals*farm_items.reproduction_rate WHERE user_land.animal_on_land=farm_items.animal_name";
    $db->query($sql);
    return "<small><b>DB-> (<i>$db->affected_rows</i> edits)</b><Br> $sql </small><BR>";

}

function farm_updateFireRisk ()
{
    global $farm_item,$db;
    $sql = "UPDATE `user_land`,`farm_items` SET user_land.risk_of_fire=user_land.risk_of_fire*farm_items.fire_risk WHERE user_land.animal_on_land=farm_items.animal_name AND user_land.risk_of_fire > '0'";
    $db->query($sql);
    return "<small><b>DB-> (<i>$db->affected_rows</i> edits)</b><Br> $sql </small><BR>";

}

function farm_addDayToCycle ()
{
    global $farm_item,$db;
    
    $sql = "UPDATE `user_land` SET `day_of_cycle`=day_of_cycle+'1' WHERE CHAR_LENGTH(planted_on_land) > '0'";
    $db->query($sql);
    return "<small><b>DB-> (<i>$db->affected_rows</i> edits)</b><Br> $sql </small><BR>";

}

function farm_subtractDayFromContract ()
{
    global $farm_item,$db;
    
    $sql = "UPDATE `user_land` SET `contract_length`=contract_length-'1'";
    $db->query($sql);
    return "<small><b>DB-> (<i>$db->affected_rows</i> edits)</b><Br> $sql </small><BR>";

}

if ($user_info[$user_id]['auth_level'] == 10)
{
    echo 'Upon completion of this script, another new day will start in Rome. Everything that needs updated at midnight should be listed here.<BR>';  
    
    echo '<h1>Farming</h1>';
    echo '<BR><BR><h2>Reproduce Animals</h2>'.farm_updateAnimals();
    echo '<BR><BR><h2>Add day to crop cycle</h2>'.farm_addDayToCycle();
    echo '<BR><BR><h2>Update Fire Risk</h2>'. farm_updateFireRisk();
    echo '<BR><BR><h2>Update Contracts (subtract 1 day)</h2>'. farm_subtractDayFromContract();
    
    $options = array();
    $options['/admin/'] = 'Return to admin screen';
    echo '<BR><BR>'.player_options($options);
}
else
{
    echo 'You are not authorised to be at this page and a not of your attempted access has been made.';
}

?>